package com.xxmw.transaction.task;

import com.xxmw.transaction.common.utils.bourse.*;
import com.xxmw.transaction.model.app.BourseApi;
import com.xxmw.transaction.model.brick.BrickOrder;
import com.xxmw.transaction.service.app.BourseApiService;
import com.xxmw.transaction.service.app.BrickOrderService;
import com.xxmw.transaction.service.app.BrickService;
import com.xxmw.transaction.service.app.SymbolSubService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: xiepeng
 * @Date: 2020-02-28 14:26
 **/
@Slf4j
@Component("brickJob")
public class BrickJob {

    @Autowired
    private BrickService brickService;

    @Autowired
    private SymbolSubService symbolSubService;

    @Autowired
    private BrickOrderService brickOrderService;

    @Autowired
    private BourseApiService bourseApiService;

    /**
     * 搬砖定时任务
     * 在表里面获取到需要搬砖的交易对
     * 获取交易对相对应的交易所价格
     * 有差值的时候进行搬砖
     */
    public void brick () {
        //获取需要搬砖的交易对
        //Set<String> symbolList = brickService.symbolList();
        //将需要搬砖的方法进行搬砖
       // log.info("搬砖策略币种数量:{}",symbolList.size());
        //symbolList.forEach(symbol -> symbolSubService.toBrick(symbol));
    }

    /**
     * 订单状态更新
     * 查询所有已提交的订单，到交易所去查询订单状态，并做更新
     */
    public void orderStatusUpdate () throws Exception {
        List<BrickOrder> orderList = brickOrderService.getOrderByStatus(1, 2, 3);
        if (null != orderList && orderList.size() > 0) {
            HashMap<Integer, BourseApi> apiMap = bourseApiService.list().stream().collect(HashMap::new, (map, api) -> map.put(api.getBourse(), api), Map::putAll);
            for (BrickOrder order : orderList) {
                switch (order.getBourse()) {
                    case 1:
                        HuobiUtil.orderInfo(apiMap.get(1), order);
                        break;
                    case 2:
                        BinanceUtil.orderInfo(apiMap.get(2), order);
                        break;
                    case 3:
                        OkexUtil.orderInfo(apiMap.get(3), order);
                        break;
                    case 4:
                        BitforexUtil.orderInfo(apiMap.get(4), order);
                        break;
                    case 5:
                        ZBUtil.orderInfo(apiMap.get(5), order);
                        break;
                    case 6:
                        PoloniexUtil.orderInfo(apiMap.get(6), order);
                        break;
                    case 7:
                        JiuBiUtil.orderInfo(apiMap.get(7), order);
                        break;
                    case 8:
                        //TODO BikiUtil.orderInfo(apiMap.get(8), order);
                        break;
                    default:
                        log.error("存在未知的交易所订单：{}", order.getBourse());
                }
                brickOrderService.updateById(order);
            }
        }
    }

}
